<?php
/**
 *  * *    author 韩天尊
 * *    Date: 2022/4/1
 * *    Email: <724700483@qq.com>
 */

namespace App\Toolys\VerifyCode;

use App\Toolys\CustomHttpException;

class VerifyCodeTools
{
    const DURATION = 5 * 60;

    // 校验验证码

    /**
     * @throws CustomHttpException
     */
    public static function checkVerifyCode($code, $time, $hash)
    {
        if ($time + self::DURATION < time()) {
            $customHttpException = new CustomHttpException();
            $customHttpException->setCode(400)->setMessage("验证码已失效");
            throw $customHttpException;
        }
        $code = strtolower($code);
        return self::getVerifyCodeHash($code, $time) == $hash;
    }

    // 生成验证码 hash 字符串
    public static function getVerifyCodeHash($code, $time)
    {
        $code = strtolower($code);
        return md5($code . $time);
    }
}